﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BabyFood.Entities;
using BabyFood.Data;

public partial class babyadmincp_ThuVienAnh_QLHinhAnh : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindCatalog.BindBoSuuTap(cboBoSuuTap);
            Pager1.CurrentIndex = 1;
            Bind_Data(Pager1.CurrentIndex);
        }
    }
    protected void Bind_Data(int PageNo)
    {

        int TotalRows = 0;
        string WhereClause = "1=1";
        WhereClause += " or AnhDaiDien like N'%" + Common.StandardString(txtTieuDe.Text) + "%' or IdBoSuuTap = " + Convert.ToInt32(cboBoSuuTap.SelectedValue);
        DataSet dsHinhAnhDetail = DataRepository.Provider.ExecuteDataSet("HinhAnh_GetPaged", WhereClause, "NgayCapNhat DESC, Id DESC", PageNo - 1, Pager1.PageSize);
        gridCatalog.DataSource = dsHinhAnhDetail.Tables[0];
        gridCatalog.DataBind();
        try
        {
            TotalRows = Convert.ToInt32(dsHinhAnhDetail.Tables[1].Rows[0][0]);
        }
        catch (Exception)
        {
        }
        Pager1.ItemCount = TotalRows;
        if (TotalRows <= Pager1.PageSize)
            Pager1.Visible = false;
        else
            Pager1.Visible = true;

    }

    protected void Pager1_Command(object sender, CommandEventArgs e)
    {
        int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
        Pager1.CurrentIndex = currnetPageIndex;
        Bind_Data(currnetPageIndex);
    }

    protected void cmdTimKiem_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            Bind_Data(Pager1.CurrentIndex);
        }
        catch (Exception ex)
        {
            Common.WriteLog("babyadmincp/ThuVienAnh/QLHinhAnh.aspx.cs", "cmdTimKiem_Click", ex.ToString());
        }
    }

    protected void cmdTimLai_Click(object sender, ImageClickEventArgs e)
    {
        txtTieuDe.Text = string.Empty;
        BindCatalog.BindBoSuuTap(cboBoSuuTap);
    }

    protected void gridCatalog_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.backgroundColor='WhiteSmoke';";
                e.Row.Attributes["onmouseout"] = "this.style.backgroundColor='White';";

                HiddenField H_IdCatalog = (HiddenField)e.Row.FindControl("H_IdCatalog");
                e.Row.Attributes.Add("ondblclick", "Javascript:DoubleClick_HinhAnh('" + H_IdCatalog.Value + "');");
            }
        }
        catch (Exception ex)
        {
            Common.WriteLog("babyadmincp/ThuVienAnh/QLHinhAnh.aspx.cs", "gridCatalog_RowDataBound", ex.ToString());
        }
    }

    protected void cmdHieuChinh_Click(object sender, ImageClickEventArgs e)
    {
        HiddenField H_IdCatalog = null;
        for (int i = 0; i < gridCatalog.Rows.Count; i++)
        {
            CheckBox chkSelect = (CheckBox)gridCatalog.Rows[i].Cells[0].FindControl("chkSelect");
            if (chkSelect.Checked)
            {
                H_IdCatalog = (HiddenField)gridCatalog.Rows[i].Cells[0].FindControl("H_IdCatalog");
                break;
            }
        }
        if (H_IdCatalog != null)
        {
            Response.Redirect("~/babyadmincp/ThuVienAnh/CNHinhAnh.aspx?id=" + H_IdCatalog.Value);
        }
    }

    protected void cmdXoa_Click(object sender, ImageClickEventArgs e)
    {
        for (int i = 0; i < gridCatalog.Rows.Count; i++)
        {
            CheckBox chkSelect = (CheckBox)gridCatalog.Rows[i].Cells[0].FindControl("chkSelect");
            if (chkSelect.Checked)
            {
                HiddenField H_IdCatalog = (HiddenField)gridCatalog.Rows[i].Cells[0].FindControl("H_IdCatalog");
                bool deleteStatus = DataRepository.HinhAnhProvider.Delete(Convert.ToInt32(H_IdCatalog.Value));
            }
        }
        Bind_Data(Pager1.CurrentIndex);
    }
    public string GetBoSuuTap(object id)
    {
        int s = Convert.ToInt32(id.ToString());
        BoSuuTap ent_BpSuuTap = DataRepository.BoSuuTapProvider.GetById(s);
        return ent_BpSuuTap.Ten;
    }
}